package com.example.abe.diandi.home.view;

import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Handler;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.example.BindView;
import com.example.abe.diandi.R;
import com.example.abe.diandi.category.model.CategoryModel;

import java.util.ArrayList;
import java.util.List;

import example.abe.com.framework.util.DensityUtil;
import example.abe.com.framework.viewinject.ViewInjectUtils;

public class MainSortPopupWindow extends PopupWindow implements View.OnClickListener {

    @BindView(R.id.view_main_sort_popup_window_ll_content)
    protected LinearLayout mLlContent;

    private View mRoot;
    private Context mContext;
    private Handler mUIHandler;
    private OnItemClickListener mListener;
    private List<TextView> mTvList;
    private List<CategoryModel> mCategoryList;

    public MainSortPopupWindow(Context context, List<CategoryModel> list) {
        super(context);
        mContext = context;
        initData(list);
        initView();
    }

    private void initData(List<CategoryModel> list) {
        mCategoryList = list;
        mTvList = new ArrayList<>();
        mUIHandler = new Handler();
    }

    private void initView() {
        mRoot = LayoutInflater.from(mContext).inflate(R.layout.view_main_sort_popup_window, null);
        ViewInjectUtils.inject(this, mRoot);
        initContentView();
        updateState(mTvList.get(0));
        setContentView(mRoot);
        setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
        setHeight(ViewGroup.LayoutParams.WRAP_CONTENT);
        setFocusable(true);
        setOutsideTouchable(true);
        setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        setAnimationStyle(R.style.TopCenterPopupWindow);
        update();
    }

    private void initContentView() {
        for (CategoryModel category : mCategoryList){
            TextView tv = new TextView(mContext);
            LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, DensityUtil.dip2px(mContext, 30));
            lp.setMargins(DensityUtil.dip2px(mContext, 12), DensityUtil.dip2px(mContext, 10), DensityUtil.dip2px(mContext, 12), 0);
            tv.setLayoutParams(lp);
            tv.setPadding(DensityUtil.dip2px(mContext, 3), 0, DensityUtil.dip2px(mContext, 3), 0);
            tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 19);
            tv.setTextColor(0xFFFFFFFF);
            tv.setGravity(Gravity.CENTER_VERTICAL);
            tv.setText(category.getTitle());
            tv.setTag(category);
            tv.setOnClickListener(this);
            mTvList.add(tv);
            mLlContent.addView(tv);
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        mListener = listener;
    }

    public void showAsDropDown(View anchor) {
        super.showAsDropDown(anchor, -anchor.getWidth()/2, DensityUtil.dip2px(mContext, -5f));
    }

    @Override
    public void onClick(View v) {
        updateState((TextView) v);
        if (mListener != null){
            mListener.onItemClick((CategoryModel)v.getTag());
        }
        mUIHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                MainSortPopupWindow.this.dismiss();
            }
        }, 200);
    }

    private void updateState(TextView v) {
        for (TextView tv: mTvList){
            tv.setTextColor(0xFFFFFFFF);
            tv.setBackground(null);
        }
        v.setTextColor(0xFF4DB8C5);
        v.setBackgroundResource(R.drawable.bg_main_sort_item_selected);
    }

    public interface OnItemClickListener {
        void onItemClick(CategoryModel categoryModel);
    }
}